package com.lbs.jiepiaisi.parser;

import com.lbs.jiepiaisi.bean.LocationBean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 时间：2018/1/11 12:09
 * 作用：
 */

public class LocationParser {
    //解析方法
    public static LocationBean parser(String jsonString){
        //Log.e("track_loc",jsonString);
        //return new Gson().fromJson(jsonString,LocationBean.class);

        LocationBean locationBean = new LocationBean();
        LocationBean.RegeoCoder regeoCoder = new LocationBean.RegeoCoder();
        LocationBean.RegeoCoder.AddressComponent addressComponent = new LocationBean.RegeoCoder.AddressComponent();
        List<LocationBean.RegeoCoder.AddressComponent.BusinessArea> businessAreaList = new ArrayList<>();
        try {
            JSONObject jsonObject=new JSONObject(jsonString);
            /**
             * 设备当前状态
             */
            if(jsonObject.getInt("status")==0){
                locationBean.setStatus(0);
            }else {
                locationBean.setStatus(1);
            }
            locationBean.setInfo(jsonObject.getString("info"));

            if(jsonObject.getString("regeocode") != null){
                JSONObject obj = jsonObject.getJSONObject("regeocode");
                if(obj.getString("formatted_address") != null){
                    regeoCoder.setFormatted_address(obj.getString("formatted_address"));
                }else {
                    regeoCoder.setFormatted_address(null);
                }
                if(obj.getString("addressComponent") != null){
                    JSONObject o = obj.getJSONObject("addressComponent");
                    if(o.getString("country") != null){
                        addressComponent.setCountry(o.getString("country"));
                    }else {
                        addressComponent.setCountry(null);
                    }
                    if(o.getString("province") != null){
                        addressComponent.setProvince(o.getString("province"));
                    }else {
                        addressComponent.setProvince(null);
                    }
                    if(o.getString("city") != null){
                        addressComponent.setCity(o.getString("city"));
                    }else {
                        addressComponent.setCity(null);
                    }
                    if(o.getString("citycode") != null){
                        addressComponent.setCitycode(o.getString("citycode"));
                    }else {
                        addressComponent.setCitycode(null);
                    }
                    if(o.getString("adcode") != null){
                        addressComponent.setAdcode(o.getString("adcode"));
                    }else {
                        addressComponent.setAdcode(null);
                    }
                    if(o.getString("towncode") != null){
                        addressComponent.setTowncode(o.getString("towncode"));
                    }else {
                        addressComponent.setTowncode(null);
                    }
                    if(o.getString("district") != null){
                        addressComponent.setDistrict(o.getString("district"));
                    }else {
                        addressComponent.setDistrict(null);
                    }
                    if(o.getString("township") != null){
                        addressComponent.setTownship(o.getString("township"));
                    }else {
                        addressComponent.setTownship(null);
                    }
                    if(o.getJSONArray("businessAreas").toString().contains("[[]]")){
                        addressComponent.setBusinessAreas(null);
                    }else {
                        if(o.getJSONArray("businessAreas") != null){
                            JSONArray jsonArrayResult = o.getJSONArray("businessAreas");

                            if(jsonArrayResult.length() > 0){
                                for(int i =0; i<jsonArrayResult.length(); i++){
                                    JSONObject jb = jsonArrayResult.getJSONObject(i);
                                    LocationBean.RegeoCoder.AddressComponent.BusinessArea businessArea = new LocationBean.RegeoCoder.AddressComponent.BusinessArea();
                                    if(jb.getString("id") != null){
                                        businessArea.setId(jb.getString("id"));
                                    }else {
                                        businessArea.setId(null);
                                    }

                                    if(jb.getString("location") != null){
                                        businessArea.setLocation(jb.getString("location"));
                                    }else {
                                        businessArea.setLocation(null);
                                    }

                                    if(jb.getString("name") != null){
                                        businessArea.setName(jb.getString("name"));
                                    }else {
                                        businessArea.setName(null);
                                    }

                                    businessAreaList.add(businessArea);
                                }
                            }else {

                            }
                        }
                        addressComponent.setBusinessAreas(businessAreaList);
                    }

                }else {
                    addressComponent.setBusinessAreas(null);
                }
                regeoCoder.setAddressComponent(addressComponent);

            }else {
                regeoCoder.setFormatted_address(null);
                regeoCoder.setAddressComponent(null);
            }
            locationBean.setRegeocode(regeoCoder);

        }catch (Exception e){
            locationBean.setStatus(1);
            locationBean.setInfo(null);
            locationBean.setRegeocode(null);
            locationBean.setInfocode(null);
        }

        return locationBean;
    }


}
